package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserLogMapper;
import com.heima.admin.service.AdUserLogService;
import com.heima.model.admin.dtos.UserLogDTO;
import com.heima.model.admin.pojos.AdUserLog;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
@Slf4j  // log
public class AdUserLogServiceImpl extends ServiceImpl<AdUserLogMapper, AdUserLog> implements AdUserLogService {
    @Override
    public ResponseResult insertLog(AdUserLog entity) {
        //2.修改
        this.save(entity);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     *   根据 name模糊查询
     *   分页查询
     *   按照排序号升序排序
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(UserLogDTO dto) {
        // 1. 检查参数
        dto.checkParam(); // 检查分页

        // 2. 封装查询参数 查询数据
        //  分页参数
        IPage<AdUserLog> pageReq = new Page<>(dto.getPage(),dto.getSize());
        //  查询参数
        LambdaQueryWrapper<AdUserLog> queryWrapper = Wrappers.<AdUserLog>lambdaQuery();
        queryWrapper.like(StringUtils.isNotBlank(dto.getName()),AdUserLog::getRequestOperation,
                dto.getName());
        queryWrapper.like(StringUtils.isNotBlank(dto.getStatus()),AdUserLog::getRequestMethod,
                dto.getStatus());

        queryWrapper.ge(StringUtils.isNotBlank(dto.getBeginDate()),
                AdUserLog::getCreatedTime, dto.getBeginDate());

        queryWrapper.le(StringUtils.isNotBlank(dto.getEndDate()),
                AdUserLog::getCreatedTime, dto.getEndDate());

        queryWrapper.orderByAsc(AdUserLog::getCreatedTime);// 升序
        // 参数1: 分页参数  参数2:查询条件
        IPage<AdUserLog> pageResult = this.page(pageReq, queryWrapper);
        // 3. 封装返回结果  PageResponseResult
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal());
        responseResult.setData(pageResult.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult delete(Integer id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询敏感词是否存在
        AdUserLog adUserLog = getById(id);
        if(adUserLog == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.删除
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
